package com.helpfuctions.gcards;

import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.ListView;

import com.pacyki.adapters.AddedContactsAdapter;
import com.pacyki.gcards.R;

public class ContactsScreen implements OnClickListener, OnItemClickListener{
	
	private ArrayList<Contact> mContacts;
	private AutoCompleteTextView mAutoCompleteTextView;
	private ArrayList<String> mContactsArray;
	private Context mContext;
	private Button mAddContactButton;
	private Contact mChosenContact;
	private ArrayList<Contact> mChosenContactsList;
	private ListView mContactsListView;
	private View mMainContactScreenView;
	
	public ContactsScreen(View mainContactScreenView, Context context, ContentResolver contentResolver) {
		ContactsList contactsList = new ContactsList(contentResolver);
		mContacts = contactsList.getContacts();
	
		mContext = context;
		
		mAutoCompleteTextView = (AutoCompleteTextView) mainContactScreenView.findViewById(R.id.contactsAutoCompleteTextView);
		mAutoCompleteTextView.setOnItemClickListener(this);

		mAddContactButton = (Button)  mainContactScreenView.findViewById(R.id.addContactButton);
		mAddContactButton.setOnClickListener(this);

		mContactsListView =(ListView)  mainContactScreenView.findViewById(R.id.contactsListView);
		
		mMainContactScreenView = mainContactScreenView;
		
		setArrayAdapter();
	}
	
	private void setArrayAdapter(){
		mContactsArray = new ArrayList<String>();
		
		for (int i = 0; i < mContacts.size(); i++) {
			Contact contact = mContacts.get(i);
			mContactsArray.add(i, contact.getName());
		}
		ArrayAdapter<String> contactsAdapter = new ArrayAdapter<String>(mContext, R.layout.contacts_textview_item, mContactsArray);
		mAutoCompleteTextView.setAdapter(contactsAdapter);
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.addContactButton:
			if (null == mChosenContactsList) {
				mChosenContactsList = new ArrayList<Contact>();
			}
			if(null != mChosenContact){
				mChosenContactsList.add(mChosenContact);
			}
			createContactList();
		break;
		
		case R.id.sendMailButton:
//			Intent i = new Intent(Intent.ACTION_SEND);
//			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			i.setType("image/jpg");
//			i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pictures/image.jpg"));
//			startActivity(i);
			break;
			
		default:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int number, long arg3) {
		String contactName = mAutoCompleteTextView.getText().toString();
		mChosenContact = findContactByContactName(contactName);
	}

	private Contact findContactByContactName(String name) {
		for (int i = 0; i < mContacts.size(); i++) {
			Contact contact = mContacts.get(i);
			Log.d("name: " + name.length(), name);
			Log.d("contact.getName(): " + contact.getName().length(), contact.getName());
			Log.d("bolean ", (name == contact.getName())+"");
			
			if (name.equals(contact.getName())){
				return contact;
			}
		}
		
		return null;
	}
	
	public void createContactList(){
		AddedContactsAdapter addedContactsAdapter = new AddedContactsAdapter(mContext, mChosenContactsList);
		mContactsListView.setAdapter(addedContactsAdapter);
	}
}
	